<?php
/**
 * 统计三星活动投票数据
 */

ini_set('display_errors', 1);
ini_set('memory_limit', -1); //不限制内存
ini_set("error_reporting", E_ALL);//显示所有错误
ob_start();
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\cache\samsungActivity\SamsungActivityCache;
use Biz\model\SamsungActivity\SamsungJoinUserListModel;
use Biz\model\SamsungActivity\SamsungVoteRecordModel;
use Biz\service\SamsungActivity\SamsungJoinUserListService;
use Doraemon\tools\Log;

try {
    $where = [
        'status' => SamsungVoteRecordModel::STATUS_NORMAL,
        "GROUP" => "obj_id",
    ];
    $countMap = SamsungVoteRecordModel::getCountMaps($where);
    $countMap = array_column($countMap, 'cnt', 'obj_id');

    $list = SamsungJoinUserListService::getShowArtworkData();
    if ($list) {
        foreach ($list as $item) {
            $totalVoteCnt = $item['vote_init'] + $countMap[$item['id']] ?? 0;
            $data = [
                'vote' => $totalVoteCnt,
            ];
            $where = [
                'id' => $item['id'],
            ];
            SamsungJoinUserListModel::updateDataByWhere($data, $where);
            $toDayNum = SamsungActivityCache::getTodayArtworkVote($item['id']);
            if ($toDayNum >= SamsungVoteRecordModel::IP_DAY_VOTE_LIMIT) {
                //钉钉机器人消息通知
                \Doraemon\service\DingTalkAlert::sendTextMsg(\Doraemon\service\DingTalkAlert::BIZ_BRAND_ACTIVITY_SAMSUNG_VOTE,
                    implode("\n", [
                        "作品 ID：".$item['id'],
                        "uid：".$item['uid'],
                        "手机号：".$item['phone'],
                        "总投票数量：".$countMap[$item['id']] ?? 0,
                        "今日投票数：".$toDayNum,
                    ])
                    , false, ['renzhifan']);
            }

        }
    }
    echo '作品投票数统计成功';
} catch (Exception $exception) {
    Log::error('CountSamsungActivityVote.php', ['exception' => $exception]);
}
